package com.hairline.controller;

import com.hairline.bean.bo.RolePermissionsBO;
import com.hairline.bean.vo.BaseRespVo;
import com.hairline.bean.Role;
import com.hairline.bean.vo.OptionsVO;
import com.hairline.bean.vo.permission.PermissionVO;
import com.hairline.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminRoleController {
    @Autowired
    AdminRoleService adminService;

    @GetMapping("role/list")
    public BaseRespVo roleList(int page, int limit, String sort, String order, String name) {
        Map<String, Object> map = adminService.roleList(page, limit, sort, order, name);
        return BaseRespVo.ok(map);
    }

    @PostMapping("role/create")
    public BaseRespVo roleCreate(@RequestBody Role role) {
        Role completeRole = adminService.roleCreate(role);
        //TODO
        if (completeRole == null) {
            return BaseRespVo.fail("用户已存在");
        }
        return BaseRespVo.ok(completeRole);
    }

    @PostMapping("role/update")
    public BaseRespVo roleUpdate(@RequestBody Role role) {
        int code = adminService.roleUpdate(role);
        if (code == 404) {
            return BaseRespVo.fail("服务器内部错误");
        }
        return BaseRespVo.ok();
    }

    @PostMapping("role/delete")
    public BaseRespVo roleDelete(@RequestBody Role role) {
        adminService.roleDelete(role);
        return BaseRespVo.ok();
    }

    //------------------------------显示全部权限和当前权限------------------------
    @GetMapping("role/permissions")
    public BaseRespVo rolePermissions(int roleId) {
        PermissionVO map = adminService.rolePermissions(roleId);
        return BaseRespVo.ok(map);
    }
    //------------------------------授权、更新role_permission关系表中内容------------------------
    @PostMapping("role/permissions")
    public BaseRespVo updatePermissions(@RequestBody RolePermissionsBO rolePermissionsBO){
        System.out.println(rolePermissionsBO);
        adminService.updatePermissions(rolePermissionsBO);
        return BaseRespVo.ok(rolePermissionsBO);
    }

    @GetMapping("role/options")
    public BaseRespVo roleOptions() {
        List<OptionsVO> list = adminService.roleOptions();
        return BaseRespVo.ok(list);
    }

//    @GetMapping("admin/list")
//    public BaseRespVo adminList(int page, int limit, String sort, String order, String username) {
//        Map<String, Object> map = adminService.adminList(page, limit, sort, order, username);
//        return BaseRespVo.ok(map);
//    }

}
